/**
 * 
 */
package fr.polichouz.geotracer.corelocator.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.polichouz.geotracer.corelocator.GeotracerServiceServerImpl;
import fr.polichouz.geotracer.shared.AlreadyTakeException;
import fr.polichouz.geotracer.shared.GeotracerService;

/**
 * @author Yoyo
 *
 */
@SuppressWarnings("serial")
public class CreateAccountServlet extends HttpServlet {

    /** Geotracer service. */
    private GeotracerService service;

    /**
     * Default constructor.
     */
    public CreateAccountServlet() {
	super();
	service = GeotracerServiceServerImpl.get();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	    throws ServletException, IOException {
	
	String username = req.getParameter("username");
	String password = req.getParameter("password");
	
	try {
	    service.createAccount(username, password);
	} catch (AlreadyTakeException ex) {
	    resp.getWriter().println("username already take");
	} catch (IllegalArgumentException ex) {
	    resp.getWriter().println("username is invalid");
	}
	
	super.doPost(req, resp);
    }
}
